<?php
/* 
 * ebiz front
 * 
 * Payment Service
 * 
 * @author fusq
 * 
 */
require_once '../app/core/services/BaseService.php';
require_once '../app/constants/AgentAccount.php';
require_once "../app/component/alipay/alipay_service.php";
require_once "../app/component/alipay/alipay_config.php";
require_once "../app/component/alipay/alipay_notify.php";
require_once '../app/models/PaymentLogDao.php';

class PaymentService extends BaseService {
	
	public function __construct(){
		parent::__construct();
		$this->paymentLogDao = new PaymentLogDao($this->dbUtil);    
	}

    public function alipayPayment($tradesum,$sn){
        $alipayinfo = AgentAccount::getAlipayInfo();
        $parameter = array(
	        "service" => "create_direct_pay_by_user",
	        "partner" =>$alipayinfo['partner'],
	        "return_url" =>$alipayinfo['return_url'],
	        "notify_url" =>$alipayinfo['notify_url'],
	        "_input_charset" => $alipayinfo['input_charset'],
	        "subject" => iconv("UTF-8","gb2312","在线充值"),
	        "body" => iconv("UTF-8","gb2312","阿基米米代理在线充值"),
	        "out_trade_no" => $sn,
	        "total_fee" => $tradesum,
	        "payment_type"=>"1",
	        "show_url" => $alipayinfo['show_url'],
	        "seller_email" => $alipayinfo['seller_email']
        );
        $alipay = new alipay_service($parameter,$alipayinfo['security_code'],$alipayinfo['sign_type']);
        $link=$alipay->create_url();
        return $link;
    }	
    
     public function getPaymentResult(){
        $alipayinfo = AgentAccount::getAlipayInfo();
        $alipay = new alipay_notify($alipayinfo['partner'],$alipayinfo['security_code'],$alipayinfo['sign_type'],$alipayinfo['input_charset'],$alipayinfo['transport']);
        $verify_result = $alipay->return_verify(); 
        return $verify_result;
     }
     
     public function getNotifyResult(){
        $alipayinfo = AgentAccount::getAlipayInfo();
        $alipay = new alipay_notify($alipayinfo['partner'],$alipayinfo['security_code'],$alipayinfo['sign_type'],$alipayinfo['input_charset'],$alipayinfo['transport']);
        $verify_result = $alipay->notify_verify(); 
        return $verify_result;
     }     
     
     public function onlinePayment($orderNo,$total,$discount,$shipFee,$logisticsType){
     	$alipayinfo = AgentAccount::getAlipayInfo();
        $parameter = array(
            "service" => "trade_create_by_buyer",
            "partner" =>$alipayinfo['partner'],
            "agent" => "",
            "return_url" =>"http://".$_SERVER[SERVER_NAME]."/o/onlinepayreturn",
            "notify_url" =>"http://".$_SERVER[SERVER_NAME]."/o/onlinepaynotify",
            "_input_charset" => $alipayinfo['input_charset'],
            "subject" => iconv("UTF-8","gb2312","Akimimi商品"),
            "body" => iconv("UTF-8","gb2312","Akimimi商品(Order Number:". $orderNo .")"),
            "out_trade_no" => $orderNo,
            "price" => $total - $shipFee,
            "quantity" => "1",
            "payment_type"=>"1",
            "show_url" => "http://www.akimimi.cn",
            "logistics_type" => $logisticsType == 2 ? "EMS" : "EXPRESS",
            "logistics_fee" => $shipFee,
            "logistics_payment" => "BUYER_PAY",    
            "logistics_type_1" => "",
            "logistics_fee_1" => "",
            "logistics_payment_1" => "",                
            "seller_email" => $alipayinfo['seller_email']
        );    
        $alipay = new alipay_service($parameter,$alipayinfo['security_code'],$alipayinfo['sign_type']);
        $link=$alipay->create_url();
        return $link;        	
     }
     
    public function insertPaymentLog($logData){
         $this->paymentLogDao->insertPaymentLog($logData);
    }
     
     public function updatePaymentLog(){
     	
     }
}